package com.axend.lib_base.base

import android.app.Application
import android.content.Context
import android.os.Handler
import android.os.Looper
import androidx.multidex.BuildConfig
import com.alibaba.android.arouter.launcher.ARouter
import com.axend.lib_base.core.BaseLibCore
import com.axend.lib_base.utils.CommUtils
import com.axend.lib_base.utils.NetWorkUtil
import dagger.hilt.android.HiltAndroidApp

/**
 * 基类的Application
 */
open class BaseApplication : Application() {

    //全局的静态Gson对象
    companion object {

        lateinit var networkType: NetWorkUtil.NetworkType   //此变量会在网络监听中被动态赋值

        //检查当前是否有网络
        fun checkHasNet(): Boolean {
            return networkType != NetWorkUtil.NetworkType.NETWORK_NO
        }

        var mCurAppVersion = "" //当前App的版本号 用于网络请求全局添加请求头

        var timeStampOffset = 0L  //设备时间与服务器时间的差值

    }

    override fun onCreate() {
        super.onCreate()

        //获取到全局的网络状态
        networkType = NetWorkUtil.getNetworkType(this@BaseApplication.applicationContext)

        //全局的 CommUtil的初始化
        BaseLibCore.init(this, Handler(Looper.getMainLooper()), android.os.Process.myTid())
        //赋值App全局版本号
        mCurAppVersion =
            packageManager.getPackageInfo(CommUtils.getContext().packageName, 0).versionName

        //ARouter初始化
        initRouter(this)
    }

    private fun initRouter(application: Application) {
        if (BuildConfig.DEBUG) {
            ARouter.openLog()
            ARouter.openDebug()
        }
        ARouter.init(application)
    }

    //Dex分包
    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
//        MultiDex.install(this)
    }

    override fun onTerminate() {
        super.onTerminate()
        BaseLibCore.unregisterNetworkObserver(this)
    }


}